package com.xl98.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xl98.entity.Role;
import com.xl98.entity.User;
import com.xl98.entity.UserRole;
import com.xl98.mapper.RoleMapper;
import com.xl98.mapper.UserMapper;
import com.xl98.mapper.UserRoleMapper;
import com.xl98.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<User> findByRole(String roleId) {
        List<User> list = new ArrayList<>();
        List<UserRole> list1 = new ArrayList<>();
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        list1 = userRoleMapper.selectList(queryWrapper);
        for(UserRole userRole : list1){
            Map<String, String> map = new HashMap<>();
            map.put("id",userRole.getUserId());
            list.add(userMapper.findByString(map));
        }
        return list;
    }

    @Override
    public User findByString(Map<String,String> map) {
        User user = userMapper.findByString(map);
        return user;
    }

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public List<User> findAllByRole(String roleId) {
        Map<String, Object> rolemap = new HashMap<>();
        Map<String, String> usermap = new HashMap<>();
        ArrayList<User> userlist = new ArrayList<>();
        List<User> list2 = new ArrayList<>();
        list2.add(null);
        rolemap.put("role_id",roleId);//角色为id
        List<UserRole> userRoles = userRoleMapper.selectByMap(rolemap);
        for (UserRole userRole : userRoles) {
            usermap.put("id", userRole.getUserId());
            userlist.add(userMapper.findByString(usermap));
            userlist.removeAll(list2);
            System.out.println(userlist);
        }
        return userlist;
    }

    @Override
    public int updateUserState(Map<String, String> map) {
        return userMapper.updateUserState(map);
    }

    @Override
    public int regist(User user) {
        Map<String, String> usermap = new HashMap<>();
        usermap.put("username",user.getUsername());
        if(userMapper.findByString(usermap)!=null){
            return 0;
        }else{
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            user.setBirthday(new Date());
            int r = userMapper.insert(user);
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name","普通用户");
            if(roleMapper.selectOne(queryWrapper)==null){
                Role role = new Role();
                role.setName("普通用户");
                roleMapper.insert(role);
            }
            Role role1 = roleMapper.selectOne(queryWrapper);
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(role1.getId());
            userRoleMapper.insert(userRole);
            return r;
        }

    }

}
